package mini.reanimate;

import java.awt.Point;

import owg.util.Calc;
import owg.util.euclidian.Compass;
import owg.util.opengl.ColorF;
import hubol.ClickableRectangle;
import hubol.Shake;
import hubol.Sprite;

public class Money extends ClickableRectangle{
	private final Point start = new Point(10, 91), end = new Point(428, start.y);
	private final Point[] visible = new Point[]{new Point(96, 194), new Point(293, 428)};
	
	private final int width = 86, height = 64;
	private boolean isVisible;
	
	private int hsp, timer;
	private final Sprite sprite = new Sprite("money");

	public Money(int x1, int y1, int w, int h) {
		super(x1, y1, w, h);
		sprite.setOrigin(Compass.NORTHWEST);
		isVisible = visible();
		hsp = 0;
		timer = 600;
	}
	
	public void step(){
		timer--;
		if (timer <= 0 && hsp == 0){
			if (x1 == start.x)
				hsp = 8;
			else
				hsp = -8;
		}
		if (hsp > 0 && x1 >= end.x){
			x1 = end.x;
			hsp = 0;
			timer = 60 + (int)Calc.random(500);
		}
		else if (hsp < 0 && x1 <= start.x){
			x1 = start.x;
			hsp = 0;
			timer = 60 + (int)Calc.random(500);
		}
		x1 += hsp;
		
		x2 = x1 + width;
		y2 = y1 + height;
		isVisible = visible();
	}
	
	private boolean visible(){
		for (Point p : visible){
			if (containedBy(p))
				return true;
		}
		return false;
	}
	
	private boolean containedBy(Point p){
		return Calc.isContained(x1 + 4, p.x, p.y) || Calc.isContained(x1 + width - 4, p.x, p.y);
	}
	
	public boolean moused(){
		if (isVisible)
			return super.moused();
		return false;
	}
	
	public boolean clicked(){
		if (isVisible)
			return super.clicked();
		return false;
	}
	
	public void render(Shake s){
		sprite.render(0, x1 + s.x, y1 + s.y, 0.671875f, 0.671875f, 0, ColorF.WHITE);
	}

}
